/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package InternetHax;


import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;


/**
 * The Menu9 is the player's currently equipped apps and is mapped to the 9 number keys.
 * @author LoginError
 */
public class Menu9 extends Menu {
    Sprite[] appImages;
    int returnValue = - 1;
    int blackFont;
    Image recharging;
    String title = "";

    public Menu9(Sprite[] appImages) {
        this.appImages = appImages;
        blackFont = Gamestate.font.makeNewFont(0xFF000000);
        recharging = Gamestate.resourceManager.getResourceByName("Recharging.gif");
    }

    synchronized public int getIndex(String title)
    {
        this.title = title;
        return getIndex();
    }
    
    synchronized public int getIndex(){
        Gamestate.modalMenu = this;
        try {
            this.wait();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
            Gamestate.modalMenu = null;
            return -1;
        }
        Gamestate.modalMenu = null;
        this.title = "";
        return returnValue;
    }
    
    synchronized public App getApp(String title)
    {
        this.title = title;
        return getApp();
    }

    synchronized public App getApp()
    {
        int index = getIndex();
        
        if(index == -1)
            return null;
                
        App[] equipment = Gamestate.theHero.getEquippedApps();
        this.title = "";
        return equipment[index];    
    }

    /**
     * So many hardcoded variables, hopefully none of them will have to change.
     * @param g
     * @param x
     * @param y
     */
    synchronized public void paint(Graphics g, int x, int y) {
        //int xoffset, yoffset;
        //xoffset = yoffset = -33;
        int key = 1;
        int appIndex = 0;
        App[] equipment = Gamestate.theHero.getEquippedApps();

        if(!title.endsWith(""))
        {
            g.setColor(Gamestate.uiBackgroundColor);
            g.fillRect(x - 50, y -64, 100, 16);

            g.setColor(Gamestate.uiDetailColor);
            g.fillRect(x  - 49, y -63, 98, 14);

            Gamestate.font.PrintString(g, x - 49, y -60, title, 0, 0,blackFont);
        }
        
        for(int yoffset = -33; yoffset <= 33; yoffset += 33){
            for (int xoffset = -33; xoffset <= 33; xoffset += 33) {
                g.setColor(Gamestate.uiBackgroundColor);
                g.fillRect(x+xoffset -9, y+yoffset -9, 20, 28);
                
                g.setColor(Gamestate.uiDetailColor);
                g.fillRect(x+xoffset -8, y+yoffset -8, 18, 26);

                g.setColor(Gamestate.uiBackgroundColor);
                g.drawLine(x+xoffset -8, y+yoffset +8, x+xoffset +10, y+yoffset +8);

                if(equipment[appIndex] != null)
                {
                    App foundApp = (App)equipment[appIndex];
                    //g.drawImage(appImage, x + xoffset -7, y + yoffset -7,0);

                    switch(foundApp.getType())
                    {
                        case Constants.APPTYPE_ACTIVE:
                            appImages[0].setPosition(x + xoffset -7, y + yoffset -8);
                            appImages[0].setFrame(foundApp.getAppID() - Constants.APPTYPE_ACTIVE);
                            appImages[0].paint(g);

                            ActiveApp app = (ActiveApp)foundApp;
                            int percent = (100*app.charges)/(app.chargesMax);

                        
                            g.setClip(x+xoffset-7, y+yoffset+10, 16 *percent /100 , 7);
                            for(int w = 0; w < 4; w++)
                            {
                                if(app.isRecharging)
                                    g.drawImage(recharging,x+xoffset-7 + (w*4), y+yoffset+10 , 0);
                                else
                                {
                                    g.setColor(0x00FF00);
                                    g.fillRect(x+xoffset-7, y+yoffset+10, 16 *percent /100 , 7);
                                }

                            }
                            g.setClip(0, 0, Gamestate.screenWidth, Gamestate.screenHeight);
                        break;

                        case Constants.APPTYPE_CONSUMABLE:
                            appImages[1].setPosition(x + xoffset -7, y + yoffset -8);
                            appImages[1].setFrame(foundApp.getAppID() - Constants.APPTYPE_CONSUMABLE);
                            appImages[1].paint(g);

                            Gamestate.font.PrintString(g, x+xoffset-6, y+yoffset+10, "x"+((ConsumableApp)foundApp).uses, 0, 0,blackFont);
                        break;
                    }
                }
                g.setColor(Gamestate.uiBackgroundColor);
                g.fillRect(x+xoffset + 7, y+yoffset+ 13, 10, 10);


                if(Gamestate.invertNumpad) //invert the numbers
                {
                 if(yoffset == -33)//top row
                     key = (xoffset + 33) / 33 + 7;

                 if(yoffset == 0)//middle row
                     key = (xoffset +33) / 33 + 4;

                 if(yoffset == 33)//bottom row
                     key = (xoffset +33) / 33 + 1;
                }
                
                Gamestate.font.PrintString(g, x+xoffset + 8, y+yoffset + 14, ""+key++ , 0, 0,Gamestate.fontColor);
                
                appIndex++;
            }
        }
    }

    synchronized public void handleInput(int keyAction) {

        switch(keyAction)
        {
            case HaxCanvas.KEY_NUM1:
                returnValue = (Gamestate.invertNumpad ? 6 : 0);
            break;

            case HaxCanvas.KEY_NUM2:
                returnValue = (Gamestate.invertNumpad ? 7 : 1);
            break;
            
            case HaxCanvas.KEY_NUM3:
                returnValue = (Gamestate.invertNumpad ? 8 : 2);
            break;
            
            case HaxCanvas.KEY_NUM4:
                returnValue = 3;
            break;
            
            case HaxCanvas.KEY_NUM5:
                returnValue = 4;
            break;
            
            case HaxCanvas.KEY_NUM6:
                returnValue = 5;
            break;
            
            case HaxCanvas.KEY_NUM7:
                returnValue = (Gamestate.invertNumpad ? 0 : 6);
            break;
            
            case HaxCanvas.KEY_NUM8:
                returnValue = (Gamestate.invertNumpad ? 1 : 7);
            break;
            
            case HaxCanvas.KEY_NUM9:
                returnValue = (Gamestate.invertNumpad ? 2 : 8);
            break;
            
            case HaxCanvas.KEY_NUM0:{ //cancel
                this.notify();
                return;
            }   
            
        }
            if(returnValue != -1)
                    this.notify();
    }
}
